package com.controller.front_controller;


import com.mypg.Question;
import com.mypg.UserHistory;
import com.mypg.UserQuestion;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.swing.plaf.SeparatorUI;
import java.util.*;

@RequestMapping("/front_question")
@Controller
public class FrontQuestionController {

    @Autowired
    QuestionService questionService;

    @Autowired
    UserService userService;

    @Autowired
    UserHistoryService userHistoryService;

    @Autowired
    UserQuestionService userQuestionService;

    @Autowired
    UserRandService userRandService;

    @Autowired
    CollectService collectService;


    @RequestMapping("/shunxu_practice_init.do")
    @ResponseBody
    public Map<String,Object> doShunxu_practice_init(String question_class, String question_chapter, String rand_flag,
                                                     String other_class,String did_class,
                                                     HttpSession session){
        Map<String,Object> map=new HashMap<>();

        //获取用户id
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();

        //根据参数获得所有试题及总试题数量
        List<Question> questionList;
        int[] count_array=null;
        int count_total;
        int rand_num=0;
        try{
            rand_num=userRandService.selectRand_numByUser_id(user_id);
        }catch(Exception e){
            rand_num=new Random().nextInt(100)+1;
            userRandService.insertUserRand(user_id,rand_num);
        }
        System.out.println("======================================================");
        if("收藏".equals(did_class)){
            questionList=collectService.selectCollectQuestionByUser_id(user_id,question_class,question_chapter);
        }else{
            if("错题".equals(did_class)){
                questionList=userQuestionService.selectWrongQuestionByUser_id(user_id,question_class,question_chapter,0);
            }else{
                questionList=questionService.selectAllQuestionByChapter(question_class,question_chapter,rand_flag,rand_num,other_class);
            }
        }
        System.out.println("======================================================");
        count_total=questionList.size();


        //获得每个章节的试题数量存入count_array
        if("科一".equals(question_class)){
            count_array=new int[5];
            for(int i=0;i<5;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
            }
        }else{
            count_array=new int[7];
            for(int i=0;i<7;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
            }
        }

        //获得用户的做题记录
        int start_index=0;
        String history_class=question_class+did_class+question_chapter;
//        System.out.println("***************************************************");
//        System.out.println(history_class);
//        System.out.println("***************************************************");
        if(question_chapter.equals("")){
            if(!"yes".equals(rand_flag)) {
                if("".equals(other_class)) {
                        history_class = question_class + "顺序练习";
                }
                else
                    history_class=question_class+other_class;
            }
            else
                history_class = question_class + "随机练习";
        }
        try{
            start_index=userHistoryService.selectIndexByUser_idAndHistory_class(user_id,history_class);
        }catch(Exception e){
            UserHistory userHistory=new UserHistory();
            userHistory.setUser_id(user_id);
            userHistory.setHistory_class(history_class);
            userHistory.setHistory_index(0);
            userHistoryService.insertUserHistory(userHistory);
        }

        map.put("data",questionList);
        map.put("count_array",count_array);
        map.put("count_total",count_total);
        map.put("start_index",start_index);
        return map;
    }

    @RequestMapping("/update_history_index.do")
    @ResponseBody
    public void doUpdate_history_index(int history_index,String question_class,String question_chapter,String rand_flag,
                                       String other_class,String did_class,
                                       HttpSession session){
        String history_class=question_class+did_class+question_chapter;
        if(question_chapter.equals("")){
            if(!"yes".equals(rand_flag)) {
                if("".equals(other_class)) {
                      history_class = question_class + "顺序练习";
                }
                else
                    history_class=question_class+other_class;
            }
            else
                    history_class = question_class + "随机练习";
        }
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        userHistoryService.updateIndexByUser_idAndHistory_class(history_index,user_id,history_class);
    }

    @RequestMapping("/chapter_exercise_init.do")
    @ResponseBody
    public int[] doChapter_exercise_init(String question_class){
        int[] count_array=null;
        if("科一".equals(question_class)){
            count_array=new int[5];
            for(int i=0;i<5;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
            }
        }else{
            count_array=new int[7];
            for(int i=0;i<7;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
            }
        }
        return count_array;
    }

    @RequestMapping("/update_user_question_table.do")
    @ResponseBody
    public int doUpdate_user_question_table(HttpSession session,int question_id,int flag_pass,
                                                           String question_class,String question_chapter){
        int res=0;
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        String user_nickname=userService.selectUserByUser_name(user_name).getUser_nickname();
        if(userQuestionService.selectUserQuestionByUserQuestion_id(user_id,question_id)==null){
            //insert
            UserQuestion userQuestion=new UserQuestion();
            userQuestion.setQuestion_id(question_id);
            userQuestion.setUser_id(user_id);
            userQuestion.setUser_nickname(user_nickname);
            userQuestion.setFlag_pass(flag_pass);
            userQuestionService.insertUserQuestion(userQuestion);
        }else{
            if(userQuestionService.selectUserQuestionByUserQuestion_id(user_id,question_id).getFlag_pass()==0){
                res=1;
            }else{
                res=2;
            }
            if(flag_pass==1){
                userQuestionService.updateFlag_passByUser_id_and_question_id(user_id,question_id,flag_pass);
            }

        }
        return res;
    }

    @RequestMapping("/flag_pass_count_init.do")
    @ResponseBody
    public Map<String,Object> doFlag_pass_count_init(HttpSession session,String question_class,String question_chapter,
                                                     String[] question_id_array){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
//        return selectFlag_pass_count(user_id,question_class,question_chapter);
        Map<String,Object> map=new HashMap<>();
        int pass_num=0;
        int fail_num=0;
        double pass_percent=0.0;
        int[] num_menu_array=new int[question_id_array.length];
        //把数组的第一个和最后一个字符串的多余部分替换掉
        question_id_array[0]=question_id_array[0].replace("[","");
        question_id_array[question_id_array.length-1]=question_id_array[question_id_array.length-1].replace("]","");
        for(int i=0;i<question_id_array.length;i++){
            try{
                int flag_pass=userQuestionService.selectUserQuestionByUserQuestion_id(user_id,Integer.parseInt(question_id_array[i])).getFlag_pass();
                if(flag_pass==0){
                    fail_num++;
                    num_menu_array[i]=0;
                }else{
                    if(flag_pass==1) {
                        pass_num++;
                        num_menu_array[i]=1;
                    }
                }
            }catch (Exception e){
                num_menu_array[i]=-1;
            }
        }
       if(pass_num!=0||fail_num!=0) {
            pass_percent = (double) pass_num / (pass_num + fail_num);
            pass_percent *= 100;
            pass_percent = (double)Math.round(pass_percent * 10) / 10;
        }
        map.put("pass_num",pass_num);
        map.put("fail_num",fail_num);
        map.put("pass_percent",pass_percent);
        map.put("num_menu_array",num_menu_array);
        return map;
    }

    public Map<String,Object> selectFlag_pass_count(int user_id,String question_class,String question_chapter){
        Map<String,Object> map=new HashMap<>();
        int pass_num=userQuestionService.selectCountByFlag_pass_and_user_id(user_id,1,question_class,question_chapter);
        int fail_num=userQuestionService.selectCountByFlag_pass_and_user_id(user_id,0,question_class,question_chapter);
        double pass_percent=0.0;
        if(pass_num!=0||fail_num!=0) {
            pass_percent = (double) pass_num / (pass_num + fail_num);
            pass_percent *= 100;
            pass_percent = (double)Math.round(pass_percent * 10) / 10;
        }
        int total_num=questionService.selectQuestion_count_For_question_table_jsp(question_class,"","");
        map.put("pass_num",pass_num);
        map.put("fail_num",fail_num);
        map.put("pass_percent",pass_percent);
        map.put("total_num",total_num);
        return map;
    }

    @RequestMapping("/init_tongji_img.do")
    @ResponseBody
    public Map<String,Object> doInit_tongji_img(HttpSession session,String question_class){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        return selectFlag_pass_count(user_id,question_class,"");
    }


    @RequestMapping("/real_test_init.do")
    @ResponseBody
    public Map<String,Object> doReal_test_init(String question_class){
        Map<String,Object> map=new HashMap<>();
        int question_total_num=10;//考试题的数量
        //题库所有题的数量
        int count_total=questionService.selectQuestion_count_For_question_table_jsp(question_class,"","");
        List<Question> questionList=new ArrayList<>();
        int[] count_array;
        int tool=question_total_num;
        int rand_num=new Random().nextInt(100)+1;
        if("科一".equals(question_class)){
            count_array=new int[5];
            for(int i=1;i<5;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
                double tool_caculate=(count_array[i]*1.0)/count_total;
                tool_caculate*=question_total_num;
                count_array[i]= (int) Math.floor(tool_caculate);
                tool-=count_array[i];
            }
            count_array[0]=tool;
        }else{
            count_array=new int[7];
            for(int i=1;i<7;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
                double tool_caculate=(count_array[i]*1.0)/count_total;
                tool_caculate*=question_total_num;
                count_array[i]= (int) Math.floor(tool_caculate);
                tool-=count_array[i];
            }
            count_array[0]=tool;
        }
//        System.out.println(Arrays.toString(count_array));
        for(int i=0;i<count_array.length;i++){
            int j=i+1;
            List<Question> questionList_tool=questionService.selectQuestionForReal_test(question_class,j+"",count_array[i],rand_num);
            questionList.addAll(questionList_tool);
        }
        map.put("data",questionList);
        return map;
    }

    @RequestMapping("/chapter_change_index.do")
    @ResponseBody
    public Map<String,Object> doChapter_change_index(String question_class){
        int[] count_array=null;
        String[] array_chapter=questionService.selectQuestion_chapter(question_class);
        if("科一".equals(question_class)){
            count_array=new int[5];
            for(int i=0;i<5;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
            }
        }else{
            count_array=new int[7];
            for(int i=0;i<7;i++){
                int j=i+1;
                count_array[i]=questionService.selectQuestion_count_For_question_table_jsp(question_class,j+"","");
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("array_count",count_array);
        map.put("array_chapter",array_chapter);
        return map;
    }

    @RequestMapping("/change_special_num.do")
    @ResponseBody
    public int[] doChange_special_num(String question_class){
        String[] array_special_str;
        if("科一".equals(question_class)){
            array_special_str=new String[]{"文字题","图片题","判断题","单选题"};
        }else{
            array_special_str=new String[]{"文字题","图片题","动画题","判断题","单选题","多选题"};
        }
        int[] array_special_count=new int[array_special_str.length];
        for(int i=0;i<array_special_str.length;i++){
            array_special_count[i]=questionService.selectCountByChapter(question_class,"","",0,
                    array_special_str[i]);
        }
        return array_special_count;
    }
}
